﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VehicleAdd : System.Web.UI.Page
    {
        protected EmpBasicInfo ebi;
        protected void Page_Load(object sender, EventArgs e)
        {
            ebi = Session["ebi"] as EmpBasicInfo;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqVehicleMains reqVehicleMains = new ReqVehicleMains();
            string maxFormNo = ctx.ReqVehicleMains.Max(r => r.FormNo);
            string formNo = "Form_Vehi_000001";
            if (!string.IsNullOrEmpty(maxFormNo))
            {
                formNo = "Form_Vehi_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
            }
            reqVehicleMains.FormNo = formNo;
            reqVehicleMains.ReqDate = DateTime.Now;
            reqVehicleMains.EmpId = ebi.Id;
            reqVehicleMains.Riders = Riders.Value;
            reqVehicleMains.RiderCnt = int.Parse(RiderCnt.Value);
            reqVehicleMains.Vehicle = Vehicle.Value;
            reqVehicleMains.PreStartTime = DateTime.Parse(PreStartTime.Value);
            reqVehicleMains.PreEndTime = DateTime.Parse(PreEndTime.Value);
            reqVehicleMains.Cause = Cause.Value;
            ctx.ReqVehicleMains.InsertOnSubmit(reqVehicleMains);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + reqVehicleMains.FormNo + "';window.close();</script>");
        }
    }
}